package code1_100.code11_20;

import java.util.HashMap;
import java.util.Map;

public class Code13 {
    //罗马数转整数
    static class Solution {
        public int romanToInt(String s) {
            char[] arr = s.toCharArray();

            int sum=0;
            Map<String,Integer> map = new HashMap<>();
            map.put("I",1);
            map.put("V",5);
            map.put("X",10);
            map.put("L",50);
            map.put("C",100);
            map.put("D",500);
            map.put("M",1000);
            for(int i=arr.length-1;i>=0;i--){
                if(i-1>=0){
                    switch(""+arr[i-1]+arr[i]){
                        case "IV": sum+=4;break;
                        case "IX": sum+=9;break;
                        case "XL": sum+=40;break;
                        case "XC": sum+=90;break;
                        case "CD": sum+=400;break;
                        case "CM": sum+=900;break;
                        default:
                            sum+=map.get(arr[i]+"");
                            i++;

                    }
                    i--;

                }else{
                    sum+=map.get(arr[i]+"");
                }


            }
            return sum;
        }
    }
}
